package com.ymatou.iapi.productimporter.testcase;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productimporter.parameter.CreateBean;
import com.ymatou.iapi.productimporter.parameter.GetTaskByFileSign;
import com.ymatou.iapi.productimporter.parameter.GetTaskByFileSignBean;
import com.ymatou.iapi.productimporter.parameter.TaobaoZhuShouExportBean;
import com.ymatou.iapi.productimporter.service.GetTaskByFileSignCall;
import com.ymatou.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductImporterCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
*根据文件签名查询任务
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTaskByFileSign{
	private static GetTaskByFileSignBean gettaskbyfilesignBean;
	private static GetTaskByFileSignCall gettaskbyfilesignCall;
	private static int vipRootUserId = AgentUsers.vipRootUserId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据文件签名查询任务");
	}

	@Before
	public void caseUp() {
		gettaskbyfilesignBean=new GetTaskByFileSignBean();
		gettaskbyfilesignCall=new GetTaskByFileSignCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettaskbyfilesignCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(int taskId,int taskStatus,CreateBean createBean){
		String data = gettaskbyfilesignCall.getJsonObject().get("data").toString();
		GetTaskByFileSign getTaskByFileSign = (GetTaskByFileSign)FormatUtil.GSONToObject(data, GetTaskByFileSign.class);
		Logger.verifyEquals(createBean.getSellerId(),getTaskByFileSign.getSellerId(),"验证sellerId");
		Logger.verifyEquals(taskId,getTaskByFileSign.getTaskId(),"验证taskId");
		Logger.verifyEquals(taskStatus,getTaskByFileSign.getTaskStatus(),"验证taskStatus");
		Logger.verifyEquals(createBean.getFileName(),getTaskByFileSign.getFileName(),"验证FileName");
		Logger.verifyEquals(createBean.getFileSign(),getTaskByFileSign.getFileSign(),"验证FileSign");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByFileSign_01() {
		Logger.start(true,"根据文件签名查询任务,0初始创建");
		try {
			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			int taskId = ProductImporterCallService.create(createBean);
			gettaskbyfilesignBean.setSellerId(vipRootUserId);
			gettaskbyfilesignBean.setAppId(TestDataManager.getRandomNum(5));
			gettaskbyfilesignBean.setFileSign(createBean.getFileSign());
			gettaskbyfilesignCall.setData(gettaskbyfilesignBean);
			gettaskbyfilesignCall.callService();			
			Logger.verifyEquals("true",gettaskbyfilesignCall.getString("success"),"验证success");
			verifyDB(taskId,0,createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByFileSign_02() {
		Logger.start(true,"根据文件签名查询任务,1--正在导入");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			gettaskbyfilesignBean.setSellerId(vipRootUserId);
			gettaskbyfilesignBean.setAppId(TestDataManager.getRandomNum(5));
			gettaskbyfilesignBean.setFileSign(createBean.getFileSign());
			gettaskbyfilesignCall.setData(gettaskbyfilesignBean);
			gettaskbyfilesignCall.callService();
			Logger.verifyEquals("true",gettaskbyfilesignCall.getString("success"),"验证success");
			verifyDB(taskId,1,createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByFileSign_03() {
		Logger.start(true,"根据文件签名查询任务,2--导入完成");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(2000);
			gettaskbyfilesignBean.setSellerId(vipRootUserId);
			gettaskbyfilesignBean.setAppId(TestDataManager.getRandomNum(5));
			gettaskbyfilesignBean.setFileSign(createBean.getFileSign());
			gettaskbyfilesignCall.setData(gettaskbyfilesignBean);
			gettaskbyfilesignCall.callService();
			Logger.verifyEquals("true",gettaskbyfilesignCall.getString("success"),"验证success");
			verifyDB(taskId,2,createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskByFileSign_04() {
		Logger.start(true,"根据文件签名查询任务,3--意外失败");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.getPacketList().get(0).setPacketUrl(upl+"c");
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(2000);
			gettaskbyfilesignBean.setSellerId(vipRootUserId);
			gettaskbyfilesignBean.setAppId(TestDataManager.getRandomNum(5));
			gettaskbyfilesignBean.setFileSign(createBean.getFileSign());
			gettaskbyfilesignCall.setData(gettaskbyfilesignBean);
			gettaskbyfilesignCall.callService();
			Logger.verifyEquals("true",gettaskbyfilesignCall.getString("success"),"验证success");
			verifyDB(taskId,3,createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetTaskByFileSign_05() {
		Logger.start(true,"根据文件签名查询任务,4--明确失败");
		try {
			String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
			TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.00", 50,picUrl);
			List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
			String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
			String upl=TestDataManager.UploadFile(s);

			CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
			createBean.setFileName("123");
			createBean.getPacketList().get(0).setPacketUrl(upl);
			int taskId = ProductImporterCallService.create(createBean);
			ProductImporterCallService.execute(taskId,createBean.getAppId());

			Thread.sleep(2000);
			gettaskbyfilesignBean.setSellerId(vipRootUserId);
			gettaskbyfilesignBean.setAppId(TestDataManager.getRandomNum(5));
			gettaskbyfilesignBean.setFileSign(createBean.getFileSign());
			gettaskbyfilesignCall.setData(gettaskbyfilesignBean);
			gettaskbyfilesignCall.callService();
			Logger.verifyEquals("true",gettaskbyfilesignCall.getString("success"),"验证success");
			verifyDB(taskId,4,createBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}